import { ref, watch } from "vue";
import { TodoListType } from "../types/todomvc";

export default function (key: string, value?: TodoListType) {
  let data = ref(value as TodoListType);

  if (value) {
    localStorage.setItem(key, JSON.stringify(value));
  } else {
    data.value = JSON.parse(localStorage.getItem(key) || "[]");
  }

  watch(data, (newValue: TodoListType) => {
    localStorage.setItem(key, JSON.stringify(newValue));
  });
  return data;
}
